
import { rule } from "@/util/validateRules";
import {checkByName,checkByKey} from '@/api/admin/sys-public-param'


let validateName = (rule, value, callback) => {
  if (window.boxType === 'edit') {
    callback()
    return;
  }
  checkByName(value)
    .then(resp => {
      if (resp.data.data > 0) {
        callback(new Error('参数名已存在'))
      } else {
        callback()
      }
    })
}
let validateKey = (rule, value, callback) => {
  if (window.boxType === 'edit') {
    callback()
    return;
  }
  checkByKey(value)
    .then(resp => {
      if (resp.data.data > 0) {
        callback(new Error('参数键已存在'))
      } else {
        callback()
      }
    })
}


export const tableOption = {
  border: true,
  index: true,
  indexLabel: '序号',
  stripe: true,
  menuAlign: 'center',
  align: 'center',
  searchMenuSpan: 6,
  column: [
    {
      label: '名称',
      search: true,
      width: 150,
      prop: 'publicName',
      rules: [
        { required: true, message: '请输名称', trigger: 'blur' },
        { max: 30, message: '长度在 30 个字符', trigger: 'blur' },
        { validator: rule.validatorNameCn, trigger: 'blur'},
        { validator: validateName, trigger: 'blur'}
      ]
    },
    {
      label: '键',
      prop: 'publicKey',
      width: 200,
      search: true,
      rules: [
        { required: true, message: '请输入键', trigger: 'blur' },
        { validator: rule.validatorKey, trigger: 'blur'},
        { validator: validateKey, trigger: 'blur'}
      ]

    },
    {
      label: '值',
      overHidden: true,
      prop: 'publicValue',
      width: 120,
      rules: [
        { required: true, message: '请输入值', trigger: 'blur' }
      ]
    },
    {
      label: '编码',
      prop: 'validateCode'
    },
    {
      label: '类型',
      prop: 'system',
      type: 'select',
      search: true,
      dicUrl: '/admin/dict/type/dict_type',
      rules: [{
        required: true,
        message: '请输入类型',
        trigger: 'blur'
      }]
    },
    {
      label: '状态',
      prop: 'status',
      width: 80,
      type: 'select',
      dicUrl: '/admin/dict/type/status_type',
      rules: [
        { required: true, message: '请输入值', trigger: 'blur' }
      ]
    },
    {
      label: '参数类型',
      prop: 'publicType',
      width: 80,
      type: 'select',
      dicUrl: '/admin/dict/type/param_type',
      rules: [{
        required: true,
        message: '请选择类型',
        trigger: 'blur'
      }]
    }
    // 省略 ...
  ]
}
